package com.lanou.quanttrade.member.service;

import com.lanou.quanttrade.member.javabean.Member;
import com.lanou.quanttrade.member.mapper.MemberMapper;
import com.lanou.quanttrade.utils.javabean.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpSession;

@Service
public class MemberService {
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 注册用户信息
     * @param param 用户名、密码、确认密码
     * @return 注册结果
     */
    public RestObject register(Member param) {
        //1、合法性校验
        String serviceMsg = "注册";
        RestObject serviceMsg1 = legitimacyVerification(param, serviceMsg);
        if (serviceMsg1 != null) return serviceMsg1;
        //1.3、确认密码必须存在
        if(param.getRepeat()==null || "".equals(param.getRepeat().trim())){
            return RestObject.ERROR(serviceMsg+"失败，请先填写确认密码！");
        }
        //1.4、密码和确认密码必须保持一致
        if(!param.getAuthstring().equals(param.getRepeat())){
            return RestObject.ERROR(serviceMsg+"失败，密码和确认密码必须保持一致！");
        }
        //2、先对密码进行MD5加密，替换未加密密码
        param.setAuthstring(
                DigestUtils.md5DigestAsHex(
                        param.getAuthstring().getBytes()
                )
        );
        //3、调用mapper层保存数据
        int rows = 0;
        try {
            rows = memberMapper.saveMemberInfo(param);
        } catch (Exception e) {
            String errorMsg = e.getMessage();
            System.out.println(errorMsg);
            if(errorMsg.contains("Duplicate entry")&& errorMsg.contains("tb_member.account")){
                //判断如果是用户名重复了，抛出用户名重复的问题
                return RestObject.ERROR("注册失败，该用户名已注册，请更换一个新的用户名！");
            }
            return RestObject.ERROR("注册失败，请联系管理员");
        }
        //4、返回业务结果
        if(rows>0){
            return RestObject.OK("注册成功！");
        }else{
            return RestObject.ERROR("注册失败，请联系管理员");
        }
    }

    /**
     * 用户名和密码的合法性校验
     * @param param
     * @param serviceMsg
     * @return
     */
    private static RestObject legitimacyVerification(Member param, String serviceMsg) {
        //1.0、防止空指针异常
        if(param == null){
            return RestObject.ERROR(serviceMsg + "失败，请联系管理员");
        }
        //1.1、用户名必须存在
        if(param.getAccount()==null || "".equals(param.getAccount().trim())){
            return RestObject.ERROR(serviceMsg + "失败，请先填写用户名！");
        }
        //1.2、密码必须存在
        if(param.getAuthstring()==null || "".equals(param.getAuthstring().trim())){
            return RestObject.ERROR(serviceMsg + "失败，请先填写密码！");
        }
        return null;
    }

    /**
     * 用户登录业务
     * @param param     用户名、密码
     * @param session   session对象
     * @return              业务结果
     */
    public RestObject login(Member param, HttpSession session) {
        //1、合法性校验
        String serviceMsg = "登录";
        RestObject serviceMsg1 = legitimacyVerification(param, serviceMsg);
        if (serviceMsg1 != null) return serviceMsg1;
        //2、对密码进行一次MD5加密
        param.setAuthstring(
                DigestUtils.md5DigestAsHex(
                        param.getAuthstring().getBytes()
                )
        );
        //3、根据用户名和密码对用户信息进行查询
        Member member = null;
        try {
            member = memberMapper.searchAccountByUserNameAndPassWord(param);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return RestObject.ERROR("登录失败，请联系管理员!");
        }
        //4、登录业务判断
        if(member!=null && member.getAccount()!=null && !"".equals(member.getAccount())){
            //登录成功
            session.setAttribute("member",member);      //session中保存了用户信息，登录成功的标志
            return RestObject.OK("登录成功");
        }else{
            //登录失败
            return RestObject.ERROR("登录失败，用户名或密码错误!");
        }
    }
}
